package ch.zbw.pra.sysventory.presentation.panels;

import java.util.Date;

import ch.zbw.pra.sysventory.application.controller.StatisticsPanelController;

import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

/**
 * Painted das Statistic Panel
 * 
 * @author Ralph Koster
 */

public class StatisticsPanel extends VerticalLayout implements Button.ClickListener {

	private static final long serialVersionUID = -1187136726930833682L;
	private StatisticsPanelController statisticsPanelController;
	private HorizontalLayout titlePanel;
	private HorizontalLayout componentsPanel;
	private VerticalLayout numberOfScansPanel;
	private VerticalLayout computersModifiedPanel;
	private HorizontalLayout errorTextPanel;
	private HorizontalLayout errorLabelPanel;
	private GridLayout secondTextPanel;
	private NativeButton showComputersModifiedButton;
	private NativeButton showNumberOfScansButton;
	private DateField dateNumberOfScansFrom;
	private DateField dateNumberOfScansTo;
	private Label numberOfScansInTimerange;
	
	public StatisticsPanel() {
		this.statisticsPanelController = new StatisticsPanelController();
		this.titlePanel = new HorizontalLayout();
		this.componentsPanel = new HorizontalLayout();
		this.numberOfScansPanel = new VerticalLayout();
		this.computersModifiedPanel = new VerticalLayout();
		
		this.setStyleName("statistics-panel");
		this.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		this.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		
		paintTitlePanel();
		paintNumberOfScansPanel();
		paintComputersModifiedPanel();
		paintComponentsPanel();
	}	

	private void paintTitlePanel() {
		titlePanel.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		titlePanel.setSpacing(true);
		titlePanel.addStyleName("titlePanel");
        
        Label title = new Label("Sysventory Hard- und Software Inventarisierung - Statistik");
        title.setSizeUndefined();
        title.addStyleName("h1");
        
        titlePanel.addComponent(title);
        titlePanel.setComponentAlignment(title, Alignment.MIDDLE_CENTER);
        titlePanel.setExpandRatio(title, 1);
        
        this.addComponent(titlePanel);
	}

	private void paintNumberOfScansPanel() {
		numberOfScansPanel.removeAllComponents();
		numberOfScansPanel.setCaption("Anzahl Scans");
		numberOfScansPanel.addStyleName("numberOfScansPanel");
		numberOfScansPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		numberOfScansPanel.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		
		//First panel with number of scans per week, month, year
		GridLayout firstTextPanel = new GridLayout(2, 1);
		GridLayout firstTextPanelRowOne = new GridLayout(1, 3);
		GridLayout firstTextPanelRowTwo = new GridLayout(1, 3);
		
		firstTextPanel.setSizeFull();
		firstTextPanel.setHeight("80px");
		firstTextPanel.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		firstTextPanel.setStyleName("firstTextPanel");
		firstTextPanelRowOne.setStyleName("firstTextPanelRowOne");
		firstTextPanelRowTwo.setStyleName("firstTextPanelRowTwo");
				
		firstTextPanelRowOne.addComponent(new Label("Anzahl Scans in der aktuellen Woche:"));
		firstTextPanelRowOne.addComponent(new Label("Anzahl Scans im aktuellen Monat:"));
		firstTextPanelRowOne.addComponent(new Label("Anzahl Scans im aktuellen Jahr:"));
		
		firstTextPanelRowTwo.addComponent(new Label(statisticsPanelController.getNumberOfScansPerWeek()));
		firstTextPanelRowTwo.addComponent(new Label(statisticsPanelController.getNumberOfScansPerMonth()));
		firstTextPanelRowTwo.addComponent(new Label(statisticsPanelController.getNumberOfScansPerYear()));
		
		firstTextPanel.addComponent(firstTextPanelRowOne);
		firstTextPanel.addComponent(firstTextPanelRowTwo);
		
		//Settings Panel with datepickers for timerange and button for show
		GridLayout settingsPanel = new GridLayout(3, 1);
		settingsPanel.setStyleName("settingsPanel");
		settingsPanel.setHeight("80px");
		settingsPanel.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		dateNumberOfScansFrom = new DateField();
		dateNumberOfScansTo = new DateField();
		dateNumberOfScansFrom.setDateFormat("dd.MM.yyyy");
		dateNumberOfScansTo.setDateFormat("dd.MM.yyyy");
		dateNumberOfScansFrom.setCaption("Datum von:");
		dateNumberOfScansTo.setCaption("Datum bis:");
		dateNumberOfScansFrom.setLenient(true);
		dateNumberOfScansTo.setLenient(true);
		showNumberOfScansButton = new NativeButton("Anzeigen");
		showNumberOfScansButton.addListener(this);
		
		settingsPanel.addComponent(dateNumberOfScansFrom);
		settingsPanel.addComponent(dateNumberOfScansTo);
		settingsPanel.addComponent(showNumberOfScansButton);
		
		//SecondTextPanel for number of scans in timerange
		secondTextPanel = new GridLayout(2, 1);
		secondTextPanel.setStyleName("settingsPanel");
		secondTextPanel.setHeight("50px");
		secondTextPanel.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		Label numberOfScansInTimerangeText = new Label("Anzahl Scans im ausgewählten Zeitraum:");
		numberOfScansInTimerange = new Label("");
		numberOfScansInTimerangeText.setWidth(90, Sizeable.UNITS_PERCENTAGE);
		numberOfScansInTimerange.setWidth(10, Sizeable.UNITS_PERCENTAGE);
		secondTextPanel.addComponent(numberOfScansInTimerangeText);
		secondTextPanel.addComponent(numberOfScansInTimerange);		
		
		paintErrorTextPanel();
		
		numberOfScansPanel.addComponent(firstTextPanel);
		numberOfScansPanel.addComponent(settingsPanel);
		numberOfScansPanel.addComponent(errorTextPanel);
		numberOfScansPanel.addComponent(secondTextPanel);
	}
	
	private void paintErrorTextPanel() {
		// errorTextPanel
		errorTextPanel = new HorizontalLayout();
		errorTextPanel.setSizeFull();
		errorTextPanel.setHeight("50px");
		
		errorLabelPanel = new HorizontalLayout();
		errorLabelPanel.setSizeFull();
		errorLabelPanel.setStyleName("errorLabelPanel");

		//icon Label
		Label iconLabel = new Label();
		iconLabel.setIcon(new ThemeResource("img/arrow.png"));
		
		//error Label
		Label errorLabel = new Label();
		errorLabel.setValue("Bitte wählen Sie die Daten für den Zeitraum aus!!");
		errorLabel.addStyleName("error-h4");
		
		errorLabelPanel.addComponent(iconLabel);
		errorLabelPanel.addComponent(errorLabel);
		
		errorLabelPanel.setExpandRatio(iconLabel, 1);
		errorLabelPanel.setExpandRatio(errorLabel, 10);
	}
	
	private void paintComputersModifiedPanel() {
		computersModifiedPanel.setCaption("Modifizierte Computer");
		computersModifiedPanel.addStyleName("computersModifiedPanel");
		computersModifiedPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		computersModifiedPanel.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		
		//Settings Panel with datepickers for timerange and button for show
		GridLayout settingsPanel = new GridLayout(3, 1);
		settingsPanel.setStyleName("settingsPanel");
		settingsPanel.setSizeFull();
		DateField dateFrom = new DateField();
		DateField dateTo = new DateField();
		dateFrom.setDateFormat("dd.MM.yyyy");
		dateTo.setDateFormat("dd.MM.yyyy");
		dateFrom.setCaption("Datum von:");
		dateTo.setCaption("Datum bis:");
		showComputersModifiedButton = new NativeButton("Anzeigen");
		
		settingsPanel.addComponent(dateFrom);
		settingsPanel.addComponent(dateTo);
		settingsPanel.addComponent(showComputersModifiedButton);
		
		//computers modified table where the modified computers from timerange are shown
		VerticalSplitPanel tablePanel = new VerticalSplitPanel();
		
		tablePanel.setStyleName("tablePanel");

		ListSelect computerTable = new ListSelect();
		computerTable.addContainerProperty("Computer Name", String.class, null);
		
		GridLayout computerDetail = new GridLayout(2, 1);
		GridLayout computerDetailLabels = new GridLayout(1, 3);
		GridLayout computerDetailValues = new GridLayout(1, 3);
		
		computerDetail.setStyleName("computerDetail");
		computerDetail.setStyleName("computerDetailLabels");
		computerDetail.setStyleName("computerDetailValues");
		
		computerDetailLabels.addComponent(new Label("Computer Name:"));
		computerDetailLabels.addComponent(new Label("Computer Type:"));
		computerDetailLabels.addComponent(new Label("Computer RAM:"));
		
		computerDetailValues.addComponent(new Label("noch nicht implementiert"));
		computerDetailValues.addComponent(new Label("noch nicht implementiert"));
		computerDetailValues.addComponent(new Label("noch nicht implementiert"));
		
		tablePanel.addComponent(computerTable);
		tablePanel.addComponent(computerDetail);
		
		computersModifiedPanel.addComponent(settingsPanel);
		computersModifiedPanel.addComponent(tablePanel);
		
	}
	
	private void paintComponentsPanel() {
		componentsPanel.setSizeFull();
		componentsPanel.setMargin(new MarginInfo(true, true, true, true));
		componentsPanel.setSpacing(true);
		componentsPanel.addComponent(numberOfScansPanel);
		componentsPanel.addComponent(computersModifiedPanel);
        
        this.addComponent(componentsPanel);
        this.setExpandRatio(componentsPanel, 1.5f);
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton() == showNumberOfScansButton) {
			dateNumberOfScansFrom.validate();
			Date startDate = (Date) dateNumberOfScansFrom.getValue();
			Date endDate = (Date) dateNumberOfScansTo.getValue();
			if(null == startDate || null == endDate) {
				numberOfScansInTimerange.setValue("");
				addErrorLabel();
			} else {
				errorTextPanel.removeComponent(errorLabelPanel);
				String numberOfScans = statisticsPanelController.getNumberOfScansInTimerange(startDate, endDate);
				numberOfScansInTimerange.setValue(numberOfScans);
			}
		}
	}
	
	private void addErrorLabel() {
		errorTextPanel.addComponent(errorLabelPanel);
	}

	public void updateGUI() {
		paintNumberOfScansPanel();
	}
}
